package org.sunspotworld.demo;

import com.sun.spot.io.j2me.radiogram.*;

//import java.text.DateFormat;
//import java.util.Date;
import javax.microedition.io.*;
import org.jfugue.*;

/**
 * Modified from the 'on Desktop' portion of the SendDataDemo. 
 * This host application collects audio information sent by the 'on SPOT'
 * portion running on neighboring SPOTs... 
 *   
 * @author: Vipul Gupta
 * modified: Ron Goldman
 */
public class SunSpotAudioBase {
    // Broadcast port on which we listen for AudioInfo packets
    private static final int HOST_PORT = 37;
    
    private StreamingPlayer player;
    private int noteValue;
    private int duration;
    private int dutyCycle;
    private int scaleType;
    private int whichBeat;
        
    private void run() throws Exception {
        RadiogramConnection rCon;
        Datagram dg;
        //DateFormat fmt = DateFormat.getTimeInstance();
        
        player = new StreamingPlayer();
        whichBeat = 0;
         
        try {
            // Open up a server-side broadcast radiogram connection
            // to listen for sensor readings being sent by different SPOTs
            rCon = (RadiogramConnection) Connector.open("radiogram://:" + HOST_PORT);
            dg = rCon.newDatagram(rCon.getMaximumLength());
        } catch (Exception e) {
             System.err.println("setUp caught " + e.getMessage());
             throw e;
        }

        // Main data collection loop
        while (true) {
            try {
                // Read sensor sample received over the radio
                rCon.receive(dg);
                String addr = dg.getAddress();
                //long time = dg.readLong();
                noteValue = dg.readInt();
                duration = dg.readInt();
                dutyCycle = dg.readInt();
                scaleType = dg.readInt();
                playBeat();
                System.out.println("From: " + addr + "   Note Value: " + Integer.toString(noteValue) + "   Duration: " + Integer.toString(duration) + "   Duty Cycle: " + Integer.toString(dutyCycle) + "%   Scale Type: " + Integer.toString(scaleType));
            } catch (Exception e) {
                System.err.println("Caught " + e +  " while reading AudioInfo packets.");
                throw e;
            }
        }
    }
    
    /**
     * Play one beat's worth of percussion. It assumes it's a new beat
     * each time it's called.
     */
    public void playBeat() {
        whichBeat = (whichBeat  % 8) + 1;
        //Determine tempo in bpm
        int tempo = (1000/duration)*60;
        switch(scaleType) {
            case 1:
                if(whichBeat == 1 || whichBeat == 2 || whichBeat == 4 || whichBeat == 5) {
                    player.stream("T" + Integer.toString(tempo) + " V9 [OPEN_HI_HAT]i+[BASS_DRUM]i");
                }
                if(whichBeat == 3  || whichBeat == 6 || whichBeat == 8) {
                    player.stream("T" + Integer.toString(tempo) + " V9 [OPEN_HI_HAT]i+[ACOUSTIC_SNARE]i"); 
                }
                else {
                    player.stream("T" + Integer.toString(tempo) + " V9 [OPEN_HI_HAT]i");
                }
                break;
            
            case 2:
                if(whichBeat != 4 && whichBeat != 8) {
                    player.stream("T" + Integer.toString(tempo) + " V9 [HAND_CLAP]i"); 
                }
                else {
                    player.stream("T" + Integer.toString(tempo) + " V1 I[FX_GOBLINS] [" + Integer.toString(noteValue+1) + "]qi [" + Integer.toString(noteValue+1) + "]qi");
                }
                break;
            
            case 3:
                if(whichBeat == 2) {
                    player.stream("T" + Integer.toString(tempo) + " V9 [RIDE_CYMBAL_1]i+[SPLASH_CYMBAL]qi");
                }
                else if(whichBeat == 4) {
                    player.stream("T" + Integer.toString(tempo) + " V9 [RIDE_CYMBAL_1]i+[VIBRASLAP]q"); 
                }
                else {
                    player.stream("T" + Integer.toString(tempo) + " V9 [RIDE_CYMBAL_1]i");
                }
                break;
            
            default:
                if(whichBeat == 3 || whichBeat == 7) {
                    player.stream("T" + Integer.toString(tempo) + " V9 [BASS_DRUM]s [BASS_DRUM]s"); 
                }
                else if(whichBeat == 4 || whichBeat == 8) {
                    player.stream("T" + Integer.toString(tempo) + " V9 [CRASH_CYMBAL_1]q");
                }
                else {
                    player.stream("T" + Integer.toString(tempo) + " V9 [BASS_DRUM]i"); 
                }
                break;
        }
    }
    
    /**
     * Start up the host application.
     *
     * @param args any command line arguments
     */
    public static void main(String[] args) throws Exception {
        SunSpotAudioBase app = new SunSpotAudioBase();
        app.run();
    }
}
